import {routerFilter} from "../../utils/routerFilter";
import util from '../../utils/util';
import consts from "../../js/const";

routerFilter({

    /**
     * 页面的初始数据
     */
    data: {
        imageUrl: consts.getImageUrl('repair-coupon.png'),
        params: {
            pageNumber: 1,
            pageSize: 5,
            typeCde: 'LC_COUPON',
            isUse : false,
            isExpire :false
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let that = this;
        const eventChannel = that.getOpenerEventChannel();
        eventChannel.on('acceptDataFromOpenerPage', function (data) {
            that.setData(that.data, () => {
                that.searchCouponByUserIdAndType();
            })
        });


    },
    searchCouponByUserIdAndType(isConcat=true) {
        let that = this;
        let data = that.data.params;
        let params = util.extend({}, data);
        util.promise('/searchCouponByUserIdAndType', params).then((res) => {
            if (res.data) {
                let couponList = res.data.list || [];
                couponList.forEach(function (item) {
                    if (item.expirationDt) {
                        item.isExpire = util.formatDate(new Date()) > item.expirationDt;
                    }
                })
                that.setData({couponList: isConcat?couponList.concat(data.couponList || []):couponList});
            }
        });
        util.promise('/sumOrderPayAmountByUserId').then((res)=>{
            let payAmount=res.data;
            if(payAmount){
                util.promise('/sumCouponAmountByUserIdAndType',params).then((res)=>{
                    if (res.data) {
                        that.setData({sumCouponAmount:res.data,balanceCouponAmount:payAmount-res.data});
                    }
                })
            }
        })

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    searchRepairCoupons(event) {
        let that = this;
        let params = that.data.params;
        params.isUse = false;
        params.isExpire = false;
        params.pageNumber = 1;
        if (event.detail.index === 1) {
            params.isUse = true;
        } else if (event.detail.index === 2) {
            params.isExpire = true;
        }
        that.data.couponList = null;
        that.setData(that.data, () => {
            that.searchCouponByUserIdAndType(false);
        })
    },
    isShowRemark(event) {
        let that = this;
        let item = event.currentTarget.dataset.item;
        let couponList = event.currentTarget.dataset.couponList;
        couponList.forEach(function (r) {
            if ((r.eycCouponRuleId === item.eycCouponRuleId && !item.eycCouponId) || (item.eycCouponId && item.eycCouponId===r.eycCouponId)) {
                r.isShowRemark = !r.isShowRemark;
            } else {
                r.isShowRemark = false;
            }
        });
        that.setData({couponList: couponList});
    },
    receiveLcCoupon(event){
        let that=this;
        this.navigateTo('/pages/my/receiveLcCoupon?maxAmount='+event.currentTarget.dataset.maxAmount,function(){
            that.searchCouponByUserIdAndType();
        },function (res){

        })
    }
})